import { makeAutoObservable } from 'mobx'
import { history, useHistory, useLocation, useParams, useRouteMatch } from 'umi'
import { finishBarrier, getBarriers } from './api'
import type { Formresult } from './interface'

class Store {
    barriersInfo?: Formresult.FormResultRes = undefined
    visible: boolean = false
    id: string = ''

    constructor() {
        makeAutoObservable(this)
        this.getBarriersInfo()
    }

    // 获取关卡信息
    getBarriersInfo = () => {
        const { id } = useParams<any>()
        const { query }: any = useLocation()
        const { resultCode, rCode } = query || {}
        let params: any = {}
        if (resultCode && resultCode !== 'undefined') {
            params = {
                result_code: rCode || resultCode,
                is_submit: 1,
            }
        }
        this.id = id
        getBarriers(id, params).then(res => {
            this.barriersInfo = res.data
            const { is_first_view } = res.data
            if (is_first_view === '1') {
                this.visible = true
            }
        })
    }

    handleFinish = () => {
        return finishBarrier(this.id).then(res => {
            this.visible = true
        })
    }

    handleClose = () => {
        this.visible = false
    }
}
export default Store
